package v1

import (
	"encoding/json"
	"fmt"
	"ginblog/model"
	"ginblog/utils"
	"ginblog/utils/errmsg"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

// AddComment 新增评论
// @Summary 新增评论
// @Description 新增评论并发送邮件通知博主
// @Tags Comment
// @Accept json
// @Produce json
//
// @Router /api/v1/comment [post]
func AddComment(c *gin.Context) {
	var data model.Comment
	_ = c.ShouldBindJSON(&data)

	code = model.AddComment(&data)
	s, _ := json.Marshal(data)
	fmt.Println(s)
	mailto := "cow11023@163.com"
	subject := "有人在你的博客留言了，请尽快审核"
	//utils.SendMail(mailto, subject, "测试一下!!!!")
	//utils.SendMail()
	utils.SendMail(mailto, subject, string(s))
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrorMsg(code),
	})
	fmt.Println(1111)
}

// GetComment 获取单个评论信息
// @Summary 获取单个评论信息
// @Description 根据评论ID获取评论信息
// @Tags Comment
// @Accept json
// @Produce json
// @Param id path int true "评论ID"
//
// @Router /api/v1/comment/{id} [get]
func GetComment(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	data, code := model.GetComment(id)
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrorMsg(code),
	})
}

// DeleteComment 删除评论
// @Summary 删除评论
// @Description 根据评论ID删除评论
// @Tags Comment
// @Accept json
// @Produce json
// @Param id path int true "评论ID"
//
// @Router /api/v1/comment/{id} [delete]
func DeleteComment(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	code = model.DeleteComment(uint(id))
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"message": errmsg.GetErrorMsg(code),
	})
}

// GetCommentCount 获取评论数量
// @Summary 获取评论数量
// @Description 获取指定文章的评论数量
// @Tags Comment
// @Accept json
// @Produce json
// @Param id path int true "文章ID"
//
// @Router /api/v1/comment/count/{id} [get]
func GetCommentCount(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	total := model.GetCommentCount(id)
	c.JSON(http.StatusOK, gin.H{
		"total": total,
	})
}

// GetCommentList 后台获取评论列表
// @Summary 后台获取评论列表
// @Description 后台获取评论列表，支持分页
// @Tags Comment
// @Accept json
// @Produce json
// @Param pagesize query int false "每页数量"
// @Param pagenum query int false "页码"
// @Router /api/v1/comment/list [get]
func GetCommentList(c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pagesize"))
	pageNum, _ := strconv.Atoi(c.Query("pagenum"))

	switch {
	case pageSize >= 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	if pageNum == 0 {
		pageNum = 1
	}

	data, total, code := model.GetCommentList(pageSize, pageNum)

	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"total":   total,
		"message": errmsg.GetErrorMsg(code),
	})

}

// 展示页面显示评论列表
func GetCommentListFront(c *gin.Context) {
	article_id, _ := strconv.Atoi(c.Param("article_id"))
	pageSize, _ := strconv.Atoi(c.Query("pagesize"))
	pageNum, _ := strconv.Atoi(c.Query("pagenum"))

	switch {
	case pageSize >= 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	if pageNum == 0 {
		pageNum = 1
	}

	data, total, code := model.GetCommentListFront(article_id, pageSize, pageNum)

	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"total":   total,
		"message": errmsg.GetErrorMsg(code),
	})

}

// 通过审核
func Checkcomment(c *gin.Context) {
	var data model.Comment
	_ = c.ShouldBindJSON(&data)
	id, _ := strconv.Atoi(c.Param("id"))

	code = model.CheckComment(id, &data)
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"message": errmsg.GetErrorMsg(code),
	})
}

// 撤下评论审核
func UnCheckcomment(c *gin.Context) {
	var data model.Comment
	_ = c.ShouldBindJSON(&data)
	id, _ := strconv.Atoi(c.Param("id"))

	code = model.UncheckComment(id, &data)
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"message": errmsg.GetErrorMsg(code),
	})
}
